/**
 * 
 */
package com.carvajal.adminpos.connectws;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.carvajal.adminpos.models.Purchase;
import com.carvajal.adminpos.models.actions.PurchaseAction;
import com.carvajal.adminpos.views.activities.purchase.PurchaseCreatePurchaseActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.widget.Toast;


/**
 * @author chenao
 *
 */
public class PurchaseConnectWs extends AsyncTask<String, Integer, Boolean>{
	
	private PurchaseCreatePurchaseActivity purchaseActivity;
	
	@Override
	protected Boolean doInBackground(String... params) {
		
		boolean resultado = true;
		HttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://moviles.heinsohn.com.co/AdminPosTest/AdminPosService/addPurchase");
     	post.setHeader("content-type", "application/json");
     	
     	//Construimos el objeto JSON compra
        JSONObject compra = new JSONObject();
        try {
        	
        	compra.put("id", params[0]);
        	compra.put("dataRegister", params[1]);
			compra.put("item", params[2]);
			compra.put("quantity", Integer.parseInt(params[3]));
			compra.put("userId", params[4]);
			
			StringEntity entity = new StringEntity(compra.toString());
	        post.setEntity(entity);
	        
	        HttpResponse resp = httpClient.execute(post);
            String respuestaWs = EntityUtils.toString(resp.getEntity());    
            
           // Toast.makeText(purchaseActivity, "Confirmacion Cancelada.", Toast.LENGTH_SHORT).show();
            
          
			
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
       
        return resultado;
	}
	
	private void imprimeResultado(String mensaje) {
	      AlertDialog.Builder dialogo =
	                new AlertDialog.Builder(purchaseActivity);
	 
	        dialogo.setMessage("Mensaje Retornado: "+mensaje)
	               .setTitle("Resultado")
	               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                       dialog.cancel();
	                   }
	               });
	 
	        dialogo.create();
	        dialogo.show();
	}

	public PurchaseCreatePurchaseActivity getPurchase() {
		return purchaseActivity;
	}

	public void setPurchase(PurchaseCreatePurchaseActivity comprasActivity) {
		this.purchaseActivity = comprasActivity;
	}


}
